package com.app.hmst.main;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Spinner;

import com.app.hmst.model.HomestayInfoVO;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

@SuppressLint({ "NewApi", "ValidFragment" }) public class GeneralInfoFragment extends Fragment{
	private View v;
	private SlidingMenu _sm;
	private boolean isEdit = false;
	private ImageView menu_icon,moreThanThreeYes,moreThanThreeNo,moreThanSixMonthYes,moreThanSixMonthNo,clickToNoti;
	private Button saveGenInfo,editGeneralInfo;
	//Mock Name of spinner
	private Spinner spinProvince,spinAumphur,spinTumbon;
	private HomestayInfoVO homestayInfoVO;
	private EditText title,address,telephone,mobile,fax,website,email,contact,sinceYears,position;
	private boolean toggleFlag;
	private String moreThan3,moreThan6;
	private RelativeLayout gotoEvaluateForm,gotoCommentPage;
	public GeneralInfoFragment(SlidingMenu sm,HomestayInfoVO homestayInfoVO){
		this._sm = sm;
		this.homestayInfoVO = homestayInfoVO;
		
	}
	public GeneralInfoFragment(SlidingMenu sm,HomestayInfoVO homestayInfoVO,Boolean toggleFlag){
		this._sm = sm;
		this.homestayInfoVO = homestayInfoVO;
		this.toggleFlag = toggleFlag;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
		System.gc();
		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
		StrictMode.setThreadPolicy(policy);
		
		v = inflater.inflate(R.layout.fragment_generalinfo_page,container, false);
		
		//Simple Evaluate "More than 3 homestay"
		moreThanThreeYes = (ImageView) v.findViewById(R.id.imageView93);
		moreThanThreeNo = (ImageView) v.findViewById(R.id.imageView10);
		editGeneralInfo = (Button) v.findViewById(R.id.edit_general_info);
		gotoEvaluateForm = (RelativeLayout) v.findViewById(R.id.relativeLayout3);
		gotoCommentPage = (RelativeLayout) v.findViewById(R.id.relativeLayout4);
		if(homestayInfoVO.q1!=null){
			if("0".equals(homestayInfoVO.q1)){
				moreThan3 = "0";
				moreThanThreeYes.setImageDrawable(getResources().getDrawable(R.drawable.selected_yes));
				moreThanThreeNo.setImageDrawable(getResources().getDrawable(R.drawable.default_no));
			}else{
				moreThan3 = "1";
				moreThanThreeNo.setImageDrawable(getResources().getDrawable(R.drawable.select_no_general));
				moreThanThreeYes.setImageDrawable(getResources().getDrawable(R.drawable.default_yes));
			}
		}else{
		moreThanThreeNo.setImageDrawable(getResources().getDrawable(R.drawable.default_no));
		moreThanThreeYes.setImageDrawable(getResources().getDrawable(R.drawable.default_yes));
		}
		
		moreThanThreeNo.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(isEdit){
					moreThanThreeNo.setImageDrawable(getResources().getDrawable(R.drawable.select_no_general));
					moreThanThreeYes.setImageDrawable(getResources().getDrawable(R.drawable.default_yes));
					moreThan3 = "1";
				}
			}
		});
		gotoCommentPage.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(Utils.isNotEmpty(moreThan3)  && Utils.isNotEmpty(moreThan6)){
    				homestayInfoVO.q1 = moreThan3;
    				homestayInfoVO.q2 = moreThan6;
    				homestayInfoVO.tel = telephone.getText().toString();
    				homestayInfoVO.name = title.getText().toString();
    				homestayInfoVO.address = address.getText().toString();
    				homestayInfoVO.mobile = mobile.getText().toString();
    				homestayInfoVO.fax = fax.getText().toString();
    				homestayInfoVO.website = website.getText().toString();
    				homestayInfoVO.email = email.getText().toString();
    				homestayInfoVO.contactName = contact.getText().toString();
    				homestayInfoVO.contactPosition = position.getText().toString();
    				homestayInfoVO.establish = sinceYears.getText().toString();
    				homestayInfoVO.save();
    				switchFragment(new FormNotePageFragment(_sm,homestayInfoVO));
    			}else{
    				AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    				builder.setMessage(getString(R.string.error_nullInfo))
    				       .setCancelable(false)
    				       .setNegativeButton(getResources().getString(R.string.homestay_sync_ok), new DialogInterface.OnClickListener() {
    				           public void onClick(DialogInterface dialog, int id) {
    				        	   dialog.dismiss();
    								
    				           }
    				       });
    				AlertDialog alert = builder.create();
    				alert.show();
    			}
			}
		});
		gotoEvaluateForm.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(Utils.isNotEmpty(moreThan3)  && Utils.isNotEmpty(moreThan6)){
    				homestayInfoVO.q1 = moreThan3;
    				homestayInfoVO.q2 = moreThan6;
    				homestayInfoVO.tel = telephone.getText().toString();
    				homestayInfoVO.name = title.getText().toString();
    				homestayInfoVO.address = address.getText().toString();
    				homestayInfoVO.mobile = mobile.getText().toString();
    				homestayInfoVO.fax = fax.getText().toString();
    				homestayInfoVO.website = website.getText().toString();
    				homestayInfoVO.email = email.getText().toString();
    				homestayInfoVO.contactName = contact.getText().toString();
    				homestayInfoVO.contactPosition = position.getText().toString();
    				homestayInfoVO.establish = sinceYears.getText().toString();
    				homestayInfoVO.save();
    				switchFragment(new EvaluateFormFragment(_sm,homestayInfoVO,true));
    			}else{
    				AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    				builder.setMessage(getString(R.string.error_nullInfo))
    				       .setCancelable(false)
    				       .setNegativeButton(getResources().getString(R.string.homestay_sync_ok), new DialogInterface.OnClickListener() {
    				           public void onClick(DialogInterface dialog, int id) {
    				        	   dialog.dismiss();
    								
    				           }
    				       });
    				AlertDialog alert = builder.create();
    				alert.show();
    			}
			}
		});
		moreThanThreeYes.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(isEdit){
					moreThanThreeNo.setImageDrawable(getResources().getDrawable(R.drawable.default_no));
					moreThanThreeYes.setImageDrawable(getResources().getDrawable(R.drawable.selected_yes));
					moreThan3 = "0";
				}
			}
		});
		clickToNoti = (ImageView) v.findViewById(R.id.imageView98);
		clickToNoti.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				switchFragment(new ViewAllEvaluatePageFragment(_sm));
			}
		});
		//Simple evaluate "More than 6 months"
		moreThanSixMonthYes = (ImageView) v.findViewById(R.id.imageView11);
		moreThanSixMonthNo = (ImageView) v.findViewById(R.id.imageView12);
		if(homestayInfoVO.q2!=null){
			if("0".equals(homestayInfoVO.q2)){
				moreThan6 = "0";
				moreThanSixMonthYes.setImageDrawable(getResources().getDrawable(R.drawable.selected_yes));
				moreThanSixMonthNo.setImageDrawable(getResources().getDrawable(R.drawable.default_no));
			}else{
				moreThan6 = "1";
				moreThanSixMonthNo.setImageDrawable(getResources().getDrawable(R.drawable.select_no_general));
				moreThanSixMonthYes.setImageDrawable(getResources().getDrawable(R.drawable.default_yes));
			}
		}else{
			moreThanSixMonthNo.setImageDrawable(getResources().getDrawable(R.drawable.default_no));
			moreThanSixMonthYes.setImageDrawable(getResources().getDrawable(R.drawable.default_yes));
		}
		
		
		
		moreThanSixMonthYes.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(isEdit){
					moreThanSixMonthNo.setImageDrawable(getResources().getDrawable(R.drawable.default_no));
					moreThanSixMonthYes.setImageDrawable(getResources().getDrawable(R.drawable.selected_yes));
					moreThan6 = "0";
				}
			}
		});
		moreThanSixMonthNo.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(isEdit){
					moreThanSixMonthNo.setImageDrawable(getResources().getDrawable(R.drawable.select_no_general));
					moreThanSixMonthYes.setImageDrawable(getResources().getDrawable(R.drawable.default_yes));
					moreThan6 = "1";
				}
			}
		});
		
		spinProvince = (Spinner) v.findViewById(R.id.spinner1);
		spinAumphur = (Spinner) v.findViewById(R.id.spinner2);
		spinTumbon = (Spinner) v.findViewById(R.id.spinner3);
		saveGenInfo = (Button) v.findViewById(R.id.imageView13);
		
		title = (EditText) v.findViewById(R.id.editText1);
		title.setText(homestayInfoVO.name);
		address = (EditText) v.findViewById(R.id.editText3);
		address.setText(homestayInfoVO.address);
		telephone = (EditText) v.findViewById(R.id.editText4); 
		telephone.setText(homestayInfoVO.tel);
		mobile = (EditText) v.findViewById(R.id.EditText01);
		mobile.setText(homestayInfoVO.mobile);
		fax = (EditText) v.findViewById(R.id.editText6);
		fax.setText(homestayInfoVO.fax);
		website = (EditText) v.findViewById(R.id.EditText03);
		website.setText(homestayInfoVO.website);
		email = (EditText) v.findViewById(R.id.EditText04);
		email.setText(homestayInfoVO.email);
		contact = (EditText) v.findViewById(R.id.EditText05); 
		contact.setText(homestayInfoVO.contactName);
		sinceYears = (EditText) v.findViewById(R.id.editText2);
		sinceYears.setText(homestayInfoVO.establish);
		position = (EditText) v.findViewById(R.id.EditText06);
		position.setText(homestayInfoVO.contactPosition);
		
		disableField(true);
		editGeneralInfo.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				saveGenInfo.setVisibility(View.VISIBLE);
				editGeneralInfo.setVisibility(View.INVISIBLE);
				disableField(false);
				isEdit=true;
			}
		});
		
		
		//Execute background process
		new InitAndLoadData().execute();
		
		//Sample province for checking size of adapter
		ArrayList<String> mockProvince = new ArrayList<String>();
		mockProvince.add(homestayInfoVO.province);
		ArrayAdapter<String> provinceAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item,mockProvince);
		provinceAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinProvince.setAdapter(provinceAdapter);
		
		
		ArrayList<String> mockAumphur = new ArrayList<String>();
		mockAumphur.add(homestayInfoVO.district);
		ArrayAdapter<String> aumphurAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item,mockAumphur);
		aumphurAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinAumphur.setAdapter(aumphurAdapter);
		
		ArrayList<String> mockTumbon = new ArrayList<String>();
		mockTumbon.add(homestayInfoVO.tumbol);
		ArrayAdapter<String> tumbonAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item,mockTumbon);
		tumbonAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinTumbon.setAdapter(tumbonAdapter);
		
		
		
		saveGenInfo.setOnClickListener(new SaveGenInfoListener());
		menu_icon = (ImageView) v.findViewById(R.id.imageView6);
		menu_icon.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				_sm.toggle();
			}
		});
		if(toggleFlag){
			_sm.toggle();
		}
		new Utils(getActivity()).onBackPressed(v, new GeneralInfoFragment(_sm,homestayInfoVO),null);

		return v;
	}
	private void switchFragment(Fragment fragment) {
		if (getActivity() == null)
			return;

		if (getActivity() instanceof HomeStayMainActivity) {
			HomeStayMainActivity fca = (HomeStayMainActivity) getActivity();
			fca.switchContent(fragment);
		}
	}
	public class SaveGenInfoListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			saveGenInfo.setVisibility(View.INVISIBLE);
			editGeneralInfo.setVisibility(View.VISIBLE);
	                if(Utils.isNotEmpty(moreThan3)  && Utils.isNotEmpty(moreThan6)){
	                	AlertDialog.Builder builder1 = new AlertDialog.Builder(getActivity());
	        	        builder1.setMessage(getString(R.string.alreadysave_message));
	        	        builder1.setCancelable(true);
	        	        builder1.setPositiveButton(getString(R.string.homestay_sync_ok),new DialogInterface.OnClickListener() {
	        	            public void onClick(DialogInterface dialog, int id) {
	        	                dialog.cancel();
			    				homestayInfoVO.q1 = moreThan3;
			    				homestayInfoVO.q2 = moreThan6;
			    				homestayInfoVO.tel = telephone.getText().toString();
			    				homestayInfoVO.name = title.getText().toString();
			    				homestayInfoVO.address = address.getText().toString();
			    				homestayInfoVO.mobile = mobile.getText().toString();
			    				homestayInfoVO.fax = fax.getText().toString();
			    				homestayInfoVO.website = website.getText().toString();
			    				homestayInfoVO.email = email.getText().toString();
			    				homestayInfoVO.contactName = contact.getText().toString();
			    				homestayInfoVO.contactPosition = position.getText().toString();
			    				homestayInfoVO.establish = sinceYears.getText().toString();
			    				homestayInfoVO.save();
			    				switchFragment(new EvaluateFormFragment(_sm,homestayInfoVO,true));
	        	            }
	        	        });
	    		        AlertDialog alert11 = builder1.create();
	    		        alert11.show();		
	    			}else{
	    				AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
	    				builder.setMessage(getString(R.string.error_nullInfo))
	    				       .setCancelable(false)
	    				       .setNegativeButton(getResources().getString(R.string.homestay_sync_ok), new DialogInterface.OnClickListener() {
	    				           public void onClick(DialogInterface dialog, int id) {
	    				        	   dialog.dismiss();
	    				        	   saveGenInfo.setVisibility(View.VISIBLE);
	    				   			   editGeneralInfo.setVisibility(View.INVISIBLE);
	    				           }
	    				       });
	    				AlertDialog alert = builder.create();
	    				alert.show();
	    			}

	
		}
		
	}
	public class InitAndLoadData extends AsyncTask<String, Void, List<String>> implements OnCancelListener{
		ProgressHUD mProgressHUD;
		
    	@Override
    	protected void onPreExecute() {
        	mProgressHUD = ProgressHUD.show(getActivity(),"Loading Content...", true,true,this);
    		super.onPreExecute();
    	}
		@Override
		protected List<String> doInBackground(String... params) {
			// TODO Auto-generated method stub			
			List<String> list = new ArrayList<String>();
			list.add("000");
			return list;
		}
		
		@Override
		protected void onPostExecute(List<String> result) {
			super.onPostExecute(result);
			if (result != null) {
				
				mProgressHUD.dismiss();
			} else {
				mProgressHUD.dismiss();
			}
			
		}
		@Override
		public void onCancel(DialogInterface dialog) {
			// TODO Auto-generated method stub
			mProgressHUD.dismiss();
		}


	}
	public void disableField(boolean isDisable){
		if(isDisable){
			title.setEnabled(false);
			address.setEnabled(false);
			telephone.setEnabled(false);
			mobile.setEnabled(false);
			fax.setEnabled(false);
			website.setEnabled(false);
			email.setEnabled(false);
			contact.setEnabled(false);
			sinceYears.setEnabled(false);
			position.setEnabled(false);
			spinProvince.setEnabled(false);
			spinAumphur.setEnabled(false);
			spinTumbon.setEnabled(false);
		}else{
			title.setEnabled(true);
			address.setEnabled(true);
			telephone.setEnabled(true);
			mobile.setEnabled(true);
			fax.setEnabled(true);
			website.setEnabled(true);
			email.setEnabled(true);
			contact.setEnabled(true);
			sinceYears.setEnabled(true);
			position.setEnabled(true);
			spinProvince.setEnabled(true);
			spinAumphur.setEnabled(true);
			spinTumbon.setEnabled(true);
		}
	
	}
}
